package com.jerp.ui.main;

import java.io.IOException;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.util.Locales;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.util.Clients;

import com.jerp.ui.auth.AuthenticationException;
import com.jerp.ui.auth.IAuthService;
import com.jerp.ui.auth.IAuthorized;
import com.jerp.ui.auth.UserCredential;

public class UserUIManager {
	private static final Logger LOG = LoggerFactory
			.getLogger(UserUIManager.class);

	private static final Locale ENG = Locales.getLocale("en"), VIE = Locales
			.getLocale("vi");
	public static String USR_CRED = "usrCred";
	private static IAuthorized ALL_ROLES = new IAuthorized() {

		@Override
		public String getName() {
			return "ALL";
		}

		@Override
		public boolean isAllowed(String roleId) {
			return true;
		}
	};

	private static IAuthService simpleAuthSrv = new IAuthService() {

		@Override
		public boolean logout(UserCredential usrCred) {
			return true;
		}

		@Override
		public UserCredential login(UserCredential usrCred)
				throws AuthenticationException {
			LOG.info("logging with UserID {} :", usrCred.getUserId());
			if ("demo".equals(usrCred.getUserId())
					&& "1234".equals(usrCred.getCredential().toString())) {
				return usrCred;
			}
			throw new AuthenticationException();
		}

		@Override
		public UserCredential authorize(UserCredential usrCred) {
			IAuthorized LimitedMenuRole = new IAuthorized() {

				@Override
				public String getName() {
					return "Even parent even child";
				}

				@Override
				public boolean isAllowed(String roleId) {
					String[] roles = roleId.split(IAuthorized.SEPERATOR);
					if (Integer.parseInt(roles[1]) % 2 != 0) {
						return false;
					}
					if (roles.length < 3) {
						return true;
					}

					if (Integer.parseInt(roles[2]) % 2 == 0) {
						return true;
					}

					return false;
				}
			};
			usrCred.setRoles(new IAuthorized[] { LimitedMenuRole });
			return usrCred;
		}
	};

	private UserCredential usrCred = new UserCredential();
	private String errMsg;

	public UserCredential getUsrCred() {
		return usrCred;
	}

	public void setUsrCred(UserCredential usrCred) {
		this.usrCred = usrCred;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public synchronized static UserCredential getCurrentUser() {
		return (UserCredential) Sessions.getCurrent(false).getAttribute(
				USR_CRED);
	}

	@Command
	@NotifyChange("errMsg")
	public void login() {
		try {
			UserCredential usrCred = simpleAuthSrv.login(this.usrCred);
			if (usrCred != null) {
				simpleAuthSrv.authorize(usrCred);
				Sessions.getCurrent(true).setAttribute(USR_CRED, usrCred);
				Executions.sendRedirect("/index.zul");
			}
		} catch (AuthenticationException ex) {
			errMsg = ex.getMessage();
		}
	}

	@GlobalCommand("toggleLang")
	public void changeLang() {
		Locale cur = Locales.getCurrent();
		System.out.println("Current language is : " + cur.getCountry());
		if (cur.equals(ENG)) {
			cur = VIE;
		} else {
			cur = ENG;
		}
		Sessions.getCurrent().setAttribute(Attributes.PREFERRED_LOCALE, cur);
		try {
			Clients.reloadMessages(cur);
			Locales.setThreadLocal(cur);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
